package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.s1995.sloth.bizs.entity.OrderItem;
import cn.s1995.sloth.bizs.entity.OrderRefund;
import cn.s1995.sloth.bizs.entity.Sku;
import cn.s1995.sloth.bizs.oo.vo.OrderItemVO;
import cn.s1995.sloth.bizs.service.OrderItemService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 订单项
 *
 * @author sloth
 * @date 2023-12-15 15:31:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/orderItem")
@Tag(description = "OrderItem", name = "BMS-订单项管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OrderItemController {

    private final OrderItemService orderItemService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param req 订单项
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_OrderItem_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject OrderItem req) {
		MPJLambdaWrapper<OrderItem> wrapper = new MPJLambdaWrapper<OrderItem>()
	            .selectAll(OrderItem.class)
	            .selectAssociation(OrderRefund.class, OrderItemVO::getOrderRefund)
	            .selectAs(Sku::getProperties, OrderItemVO::getProperties)
	            .leftJoin(OrderRefund.class, OrderRefund::getOrderItemId, OrderItem::getId)
	            .leftJoin(Sku.class, Sku::getId, OrderItem::getSkuId)
	            .leftJoin(OrderRefund.class, OrderRefund::getOrderItemId, OrderItem::getId)
	            .eq(Objects.nonNull(req.getOrderId()), OrderItem::getOrderId, req.getOrderId());
	    return R.ok(orderItemService.selectJoinListPage(page, OrderItemVO.class, wrapper));
//        LambdaQueryWrapper<OrderItem> wrapper = Wrappers.lambdaQuery();
//		wrapper.eq(StrUtil.isNotBlank(req.getOrderId()), OrderItem::getOrderId, req.getOrderId());
//        return R.ok(orderItemService.page(page, wrapper));
    }


    /**
     * 通过id查询订单项
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_OrderItem_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(orderItemService.getById(id));
    }

    /**
     * 新增订单项
     * @param req 订单项
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增订单项", description = "新增订单项")
    @SysLog("新增订单项")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderItem_add')")
    public R save(@RequestBody OrderItem req) {
        return R.ok(orderItemService.save(req));
    }

    /**
     * 修改订单项
     * @param req 订单项
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改订单项", description = "修改订单项")
    @SysLog("修改订单项")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderItem_edit')")
    public R updateById(@RequestBody OrderItem req) {
        return R.ok(orderItemService.updateById(req));
    }

    /**
     * 通过id删除订单项
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除订单项", description = "通过id删除订单项")
    @SysLog("通过id删除订单项")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderItem_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(orderItemService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_OrderItem_export')")
    public List<OrderItem> export(OrderItem req, Long[] ids) {
        return orderItemService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), OrderItem::getId, ids));
    }
}
